#include "stdafx.h"
#include "hereUIShowThread.h"

hereUIShowThread::hereUIShowThread(void)
:hereBaseThread()
{
	memset(m_modulePath, 0, sizeof(m_modulePath));
	::GetModuleFileName(NULL, m_modulePath, MAX_PATH);
	::PathRemoveFileSpec(m_modulePath);
	::PathAddBackslash(m_modulePath);

}

hereUIShowThread::~hereUIShowThread(void)
{
}

DWORD hereUIShowThread::cusStart()
{
	if ( m_runthread )
		return Resume();
	else
		return Start();
}

DWORD hereUIShowThread::ThreadMethod()
{
	char *p = NULL;
	char cc_shell[MAX_PATH] = {0};
	DO_SHELL_RESULT shell_Ret = DO_SHELL_FAIL;


	while (true)
	{
		// no need
		//sprintf_s(cc_shell, sizeof(cc_shell), "%shereAndroid.cc wait-for-device", m_modulePath);
		//if ( m_hBaseShell.doShell_Result(cc_shell, &p) < 0)
		//	continue;

		shell_Ret = startAndroidActivity();

		if ( DO_SHELL_DEVICE_NOTFOUND == shell_Ret )
		{
			m_MsgParamW.failCD = shell_Ret;
			PostMessage(m_hWnd, HERE_CUSTMSG_ANDROID_CONNECT_FAIL, (WPARAM)&m_MsgParamW, 0);
			Suspend();
			continue;
		}

		if ( DO_SHELL_ACTIVITY_NOTEXIST == shell_Ret )
		{
			installAndroidActivity();
			continue;

		}
		else if ( DO_SHELL_ACTIVITY_SUCC == shell_Ret )
		{
			androidADBInstall();
		}
		else
		{
			m_MsgParamW.failCD = ANDROID_CONNECT_FAIL;
			PostMessage(m_hWnd, HERE_CUSTMSG_ANDROID_CONNECT_FAIL, (WPARAM)&m_MsgParamW, 0);
			Suspend();
			continue;
		}

		if ( !m_hAndroidCore.isConnected() && m_hAndroidCore.connect(HERE_ANDROID_DSTADDR, HERE_ANDROID_DSTPORT) < 0 )
		{
			m_MsgParamW.failCD = ANDROID_CONNECT_FAIL;
			PostMessage(m_hWnd, HERE_CUSTMSG_ANDROID_CONNECT_FAIL, (WPARAM)&m_MsgParamW, 0);
			Suspend();
			continue;
		}
		PostMessage(m_hWnd, HERE_CUSTMSG_ANDROID_CONNECT_SUCC, 0, 0);

		Sleep(1000);
		char *gotstr = m_hAndroidCore.GetAndroidContact();
		if ( NULL != gotstr )
				strcpy( m_MsgParamL.contact, gotstr );
		else
			strcpy( m_MsgParamL.contact, _T("Get failed!") );
		PostMessage(m_hWnd, HERE_CUSTMSG_ANDROID_PARAM_GOT, 0, (LPARAM)&m_MsgParamL);
		Suspend();

	}
	return 0;
}

DO_SHELL_RESULT hereUIShowThread::startAndroidActivity()
{
	char *p = NULL;
	char cc_shell[MAX_PATH] = {0};

	sprintf_s(cc_shell, sizeof(cc_shell), "%shereAndroid.cc shell am start -n here.android.herehelper/.MainActivity", m_modulePath);
	if ( m_hBaseShell.doShell_Result(cc_shell, &p) < 0)
		return DO_SHELL_FAIL;

	if ( strstr(p, "device not found") )
		return DO_SHELL_DEVICE_NOTFOUND;

	if ( strstr(p, "herehelper.MainActivity} does not exist") )
		return DO_SHELL_ACTIVITY_NOTEXIST;

	if ( strstr(p, "Starting: Intent { cmp=here.android.herehelper/.MainActivity }") )
		return DO_SHELL_ACTIVITY_SUCC;

	return DO_SHELL_SUCC;
}

DO_SHELL_RESULT hereUIShowThread::installAndroidActivity()
{
	char *p = NULL;
	char cc_shell[MAX_PATH] = {0};

	sprintf_s(cc_shell, sizeof(cc_shell), "%shereAndroid.cc install -r %shereHelper.apk", m_modulePath, m_modulePath);
	if ( m_hBaseShell.doShell_Result(cc_shell, &p) < 0)
		return DO_SHELL_FAIL;

	if ( strstr(p, "device not found") )
		return DO_SHELL_DEVICE_NOTFOUND;

	if ( strstr(p, "Success") )
		return DO_SHELL_INSTALL_SUCC;

	return DO_SHELL_SUCC;
}

void hereUIShowThread::androidADBInstall()
{
	char *p = NULL;
	char cc_shell[MAX_PATH] = {0};

	sprintf_s(cc_shell, sizeof(cc_shell), "%shereAndroid.cc forward tcp:%u tcp:%u", m_modulePath, HERE_ANDROID_DSTPORT, HERE_ANDROID_DSTREMOTE);
	m_hBaseShell.doShell_Result(cc_shell, &p);
}